﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using Service.Core;
using Service.Core.Interface;
using Service.Core.Result;
using Web.BML.AnalysisData;
using Web.BML.CoreData;
using Web.Models.AnalysisData;
using Web.Models.CoreData;
using Web.ViewModel.CoreData;
using Tools;
using System.IO;
using System.Xml;

namespace Web.Service
{
    public  class GalleryService : BaseService
    {
        
        
        private GalleryOp galleryOp = GalleryOp.GetInstance();

        private static GalleryService instance;

        private GalleryService()
        {

        }

        public static GalleryService GetInstance()
        {
            if (instance == null)
            {
                instance = new GalleryService();
            }
            return instance;
        }

        /// <summary>
        /// 查询用户的图片
        /// </summary>
        /// <param name="id">Company的id</param>
        /// <returns></returns>
        public List<Gallery> QueryConfigs(String companyId)
        {
            List<Gallery> gallery = galleryOp.QueryByCompanyId(companyId);
            return gallery;
        }

        /// <summary>
        /// 查询所有的图片
        /// </summary>
        /// <returns></returns>
        public List<Gallery> QueryAll()
        {
            List<Gallery> gallery = galleryOp.QueryAll();
            return gallery;
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="id">图片id</param>
        /// <returns></returns>
        public IServiceResult DeleteImage(String id)
        {
            if(String.IsNullOrEmpty(id))
            {
                return Result(ResultType.ParamIsNull, "请求的参数错误");
            }
            Gallery model = galleryOp.Query(id);
            if(model==null)
            {
                return Result(ResultType.TargetIsNotFound,"指定的图片已删除或不存在");
            }
            galleryOp.DeleteGallery(model);
            return Result(ResultType.Success, "操作成功");
        }

        /// <summary>
        /// 更新图片信息
        /// </summary>
        /// <param name="gallery"></param>
        /// <returns></returns>
        public IServiceResult ModifyImage(Gallery gallery)
        {
            if(gallery==null)
            {
                return Result(ResultType.ParamIsNull,"您当前操作异常，请尝试刷新页面重新操作");
            }
            if(ParamsIsNull(gallery.Id, gallery.ImageName, gallery.Description))
            {
                return Result(ResultType.ParamIsNull, "您当前操作异常，请尝试刷新页面重新操作");
            }
            Gallery model = galleryOp.Query(gallery.Id);
            model.ImageName = gallery.ImageName;
            model.Description = gallery.Description;
            galleryOp.ModifGallery(model);
            return Result(ResultType.Success,"操作成功");
        }

        /// <summary>
        /// 上传用户图片
        /// </summary>
        /// <param name="type">1:LOGO图片，2:客户端初始化图片</param>
        /// <returns></returns>
        public IServiceResult UploadUserImg(String companyId, String filename,String id)
        {
            if (string.IsNullOrEmpty(filename) || string.IsNullOrEmpty(companyId))
            {
                return Result(ResultType.ParamIsNull);
            }
            Company company = CompanyOp.GetInstance().QueryCompany(companyId);
            galleryOp.CreateGallery(new Gallery
            {
                CompanyId=companyId,
                CompanyName=company.Name,
                FilePath=filename,
                Id=id
            });
            return Result(ResultType.Success);
        }

        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="filenames">图片路径</param>
        /// <returns></returns>
        public IServiceResult DownLoadImage(List<string> filenames)
        {
            if (ParamsIsNull(filenames))
            {
                return Result(ResultType.ParamIsNull,"请选择图片");
            }
            
            for (var i=0;i<filenames.Count;i++)
            {
                filenames[i] = HttpContext.Current.Server.MapPath("~") + filenames[i];
            }
            string dstPath = HttpContext.Current.Server.MapPath("~") + "Upload\\Gallery\\Images.zip";
            FileHelper.ZipFiles(filenames, dstPath);
            return Result(ResultType.Success, dstPath.Replace(HttpContext.Current.Server.MapPath("~"), "/"), null);
        }





    }
}